package com.rocky.study.threadlocal;

import java.util.concurrent.atomic.AtomicLong;

public class ThreadLocalDemo01 {

    private static final AtomicLong a1 = new AtomicLong();

    private static final ThreadLocal<Long> local = new ThreadLocal<Long>() {
        @Override
        protected Long initialValue() {
            return a1.getAndIncrement();
        }
    };

    public static long get() {
        return local.get();
    }

    public static class Task implements Runnable {

        @Override
        public void run() {
            System.out.println("当前线程是: " + Thread.currentThread().getName() + "; 分配的id为:" + get());
        }
    }

    public static void main(String[] args) {
        Task task = new Task();

        new Thread(task, "关羽").start();
        new Thread(task, "张飞").start();
        new Thread(task, "刘备").start();
    }
}
